package com.xudow.app.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.activeshare.edu.ucenter.models.vo.AttendanceVO;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.ui.adapter.ClazzLogAdapter;
import com.xudow.app.ui.support.RecyclerViewScrollLocationListener;
import com.xudow.app.ui.support.XLinearLayoutManager;
import com.xudow.app.ui.task.ClazzLogTask;
import com.xudow.app.util.Maps;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static com.xudow.app.util.LogUtils.LOGE;
import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 考勤记录Fragment
 *
 * @author lukecheng
 */
public class ClazzLogFragment extends BaseFragment {
    private static final String TAG = makeLogTag(ClazzLogFragment.class);

    private String type;

    private List<AttendanceVO> logs;
    private ClazzLogAdapter dataAdapter;

    private RecyclerView dataRecyclerView;
    private LinearLayout footerLayout;
    private RelativeLayout emptyLayout;

    private ClazzLogTask clazzLogTask;

    private String studentId;

    private int page = 1;

    static ClazzLogFragment newInstance(String studentId, String timeType) {
        ClazzLogFragment f = new ClazzLogFragment();

        Bundle args = new Bundle();
        args.putString("type", timeType);
        args.putString("studentId", studentId);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        type = getArguments().getString("type");
        studentId = getArguments().getString("studentId");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_clazz_log, container, false);

        footerLayout = (LinearLayout) v.findViewById(R.id.footer);

        emptyLayout = (RelativeLayout) v.findViewById(R.id.enroll_require_prompt);

        dataRecyclerView = (RecyclerView) v.findViewById(R.id.data_list);
        dataRecyclerView.setHasFixedSize(true);

        XLinearLayoutManager layoutManager = new XLinearLayoutManager(getActivity());
        layoutManager.setOnRecyclerViewScrollLocationListener(dataRecyclerView,
                new RecyclerViewScrollLocationListener() {
                    @Override
                    public void onTopWhenScrollIdle(RecyclerView recyclerView) {
                    }

                    @Override
                    public void onBottomWhenScrollIdle(RecyclerView recyclerView) {
                    }
                });
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        dataRecyclerView.setLayoutManager(layoutManager);

        logs = new ArrayList<AttendanceVO>();
        dataAdapter = new ClazzLogAdapter(logs);
        dataRecyclerView.setAdapter(dataAdapter);

        loadContent();

        return v;
    }

    /**
     * 加载记录
     */
    private void loadContent() {
        if (null == studentId) {
            initEmptyData();

            return;
        }

        HashMap<String, String> params = Maps.newHashMap();
        params.put("studentid", this.studentId);
        params.put("type", this.type);
        params.put(Config.PAGINATION_OFFSET_PARAM_NAME, "9");
        params.put(Config.PAGINATION_PARAM_NAME, "" + page);

        clazzLogTask = new ClazzLogTask(getActivity(), logHandler);
        clazzLogTask.execute(params);
    }

    private Handler logHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            footerLayout.setVisibility(View.GONE);

            if (ClazzLogTask.SUCCESS == msg.what) {
                onLoadSuccess(msg.getData());
            } else {
                initEmptyData();
            }
        }
    };

    private void onLoadSuccess(Bundle data) {
        AttendanceVO[] attendances = (AttendanceVO[]) data.getSerializable("attendances");

        if (attendances == null || attendances.length <= 0) {
            initEmptyData();
            return;
        }

        emptyLayout.setVisibility(View.GONE);
        dataRecyclerView.setVisibility(View.VISIBLE);

        logs.addAll(Arrays.asList(attendances));
        dataAdapter.notifyDataSetChanged();
    }

    private void initEmptyData() {
        emptyLayout.setVisibility(View.VISIBLE);
        dataRecyclerView.setVisibility(View.GONE);
    }

    @Override
    public void onDestroy() {
        if (null != clazzLogTask && !clazzLogTask.isCancelled()) {
            clazzLogTask.cancel(true);
            clazzLogTask = null;
        }

        super.onDestroy();
    }
}